package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MyMailList;
import com.prs.crm.domain.oa.MyMailListType;
import com.prs.crm.domain.oa.Notice;
import com.prs.crm.domain.oa.PublicMailListType;
import com.prs.crm.service.oa.MyMailListService;
import com.prs.crm.service.oa.MyMailListTypeService;
import com.prs.crm.service.oa.PublicMailListService;
import com.prs.crm.service.oa.PublicMailListTypeService;

public class PublicMailListTypeAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private PaginationSupport pagination;
	private Integer[] ids;
	private PublicMailListType publicMailListType;
	private PublicMailListTypeService publicMailListTypeService;
	private PublicMailListService publicMailListService;
	
	@Action("listpublicMailListType")
	public String listpublicMailListType(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PublicMailListType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPublicMailListTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("editpublicMailListType")
	public String editpublicMailListType(){
		return SUCCESS;
	}
	
	@Action(value = "removepublicMailListType", results = {
			@Result(name = "success", type = "redirect", location = "listpublicMailListType"),
			@Result(name = "error", type = "chain", location = "listpublicMailListType") })
	public String removepublicMailListType() {
		if (ids == null) {
			this.addActionError("请选择要删除的通讯录类别");
			return ERROR;
		}
		for (Integer id : ids) {
			PublicMailListType type = this.getPublicMailListTypeService().get(id);
			if (this.getPublicMailListService().getPublicMailListByType(type).size() != 0) {
				this.addActionError("该类型包含数据,无法删除");
				return ERROR;
			}
		}
		this.getPublicMailListTypeService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action(value = "savepublicMailListType", results = {
			@Result(name = "success", type = "redirect", location = "listpublicMailListType"),
			@Result(name = "error", type = "chain", location = "editpublicMailListType") })
	public String savepublicMailListType() {
		if(publicMailListType.getTitle() == null || publicMailListType.getTitle().trim().equals("")){
			this.addActionError("请输入类型名");
			return ERROR;
		}
		Person person = AppContext.getAccountContext().currentUser();
		if(this.getPublicMailListTypeService().getTypeByTitle(publicMailListType.getTitle(),person) != null){
			this.addActionError("该类型已存在");
			return ERROR;
		}
		publicMailListType.setPerson(person);
		this.getPublicMailListTypeService().save(publicMailListType);
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public PublicMailListType getPublicMailListType() {
		return publicMailListType;
	}

	public void setPublicMailListType(PublicMailListType publicMailListType) {
		this.publicMailListType = publicMailListType;
	}

	public PublicMailListTypeService getPublicMailListTypeService() {
		return publicMailListTypeService;
	}

	public void setPublicMailListTypeService(
			PublicMailListTypeService publicMailListTypeService) {
		this.publicMailListTypeService = publicMailListTypeService;
	}

	public PublicMailListService getPublicMailListService() {
		return publicMailListService;
	}

	public void setPublicMailListService(PublicMailListService publicMailListService) {
		this.publicMailListService = publicMailListService;
	}


}
